/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户访问控制 判断用户有无session 或者是判断用户访问是不是在UI框架中
 *
 * @author liangdelin
 */
public class RequestFilter implements Filter {

    private HttpServletRequest httpRequest;
    private HttpServletResponse httpResponse;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        this.httpRequest = (HttpServletRequest) request;
        this.httpResponse = (HttpServletResponse) response;
//        System.out.println("url filter = \t" + httpRequest.getRequestURI());
//        String referStr = httpRequest.getHeader("Referer");
//        System.out.println("----referStr\t" + referStr);
//        if (judgeRefer(referStr)) {
//            System.out.println("continue" + referStr);
//            chain.doFilter(httpRequest, httpResponse);
//        } else {
//            System.out.println("=======false");
//            httpResponse.sendRedirect("/WebManage");
//        }

        String requestURL = httpRequest.getRequestURI();
        System.out.println("URL" + requestURL);

        if (requestURL.matches(".*login")) {
            System.out.println("create   session ");
            chain.doFilter(httpRequest, httpResponse);
        } else if (requestURL.matches(".*html")) {
            if (judgeSession(httpRequest)) {
                System.out.println("have   session ");
                chain.doFilter(httpRequest, httpResponse);
            } else {
                System.out.println("not have session");
                httpResponse.sendRedirect("/WebManage/404.html");
            }
        } else {
            System.out.println("init load");
            chain.doFilter(httpRequest, httpResponse);
        }
    }

    @Override
    public void destroy() {

    }

    /**
     * 判断用户的Session时候存在
     *
     * @param request
     * @return
     */
    public static boolean judgeSession(HttpServletRequest request) {
        String username = null;
        HttpSession session = request.getSession(false);  //get user session
        try {
            username = session.getAttribute("name").toString();
            if (username == null) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
    }

//    /**
//     * 判断用户的URL的访问参考页 判断用户的参考页是否带有WebManage
//     *
//     * @param refer
//     * @return
//     */
//    public static boolean judgeRefer(String refer) {
//        if (refer == null) {
//            return false;
//        }
//        Pattern p = Pattern.compile(".*WebManage.*");
//        Matcher mm = p.matcher(refer);
//        return mm.matches();
//    }
}
